package com.hwadee.property.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hwadee.property.pojo.Building;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface BuildingMapper extends BaseMapper<Building> {
    // 计算总记录数
    @Select("<script>" +
            "SELECT COUNT(*) FROM building " +
            "<where>" +
            "   <if test='communityId != null'> AND community_id = #{communityId} </if>" +
            "   <if test='buildingNumber != null'> AND building_number = #{buildingNumber} </if>" +
            "   <if test='buildingUnit != null'> AND building_unit = #{buildingUnit} </if>" +
            "   <if test='floorCount != null'> AND floor_count = #{floorCount} </if>" +
            "   <if test='households != null'> AND households = #{households} </if>" +
            "   <if test='location != null and location.trim() != \"\"'> " +
            "       AND location LIKE CONCAT('%', #{location}, '%') " +
            "   </if>" +
            "   <if test='houseType != null and houseType.trim() != \"\"'> " +
            "       AND house_type LIKE CONCAT('%', #{houseType}, '%') " +
            "   </if>" +
            "</where>" +
            "</script>")
    int countBuildings(@Param("communityId") Long communityId,
                       @Param("buildingNumber") Long buildingNumber,
                       @Param("buildingUnit") Integer buildingUnit,
                       @Param("floorCount") Integer floorCount,
                       @Param("households") Integer households,
                       @Param("location") String location,
                       @Param("houseType") String houseType);

    // 分页查询
    @Select("<script>" +
            "SELECT * FROM building " +
            "<where>" +
            "   <if test='communityId != null'> AND community_id = #{communityId} </if>" +
            "   <if test='buildingNumber != null'> AND building_number = #{buildingNumber} </if>" +
            "   <if test='buildingUnit != null'> AND building_unit = #{buildingUnit} </if>" +
            "   <if test='floorCount != null'> AND floor_count = #{floorCount} </if>" +
            "   <if test='households != null'> AND households = #{households} </if>" +
            "   <if test='location != null and location.trim() != \"\"'> " +
            "       AND location LIKE CONCAT('%', #{location}, '%') " +
            "   </if>" +
            "   <if test='houseType != null and houseType.trim() != \"\"'> " +
            "       AND house_type LIKE CONCAT('%', #{houseType}, '%') " +
            "   </if>" +
            "</where> " +
            "ORDER BY id ASC " +
            "LIMIT #{size} OFFSET #{offset}" +
            "</script>")
    List<Building> getBuildings(@Param("communityId") Long communityId,
                                @Param("buildingNumber") Long buildingNumber,
                                @Param("buildingUnit") Integer buildingUnit,
                                @Param("floorCount") Integer floorCount,
                                @Param("households") Integer households,
                                @Param("location") String location,
                                @Param("houseType") String houseType,
                                @Param("size") int size,
                                @Param("offset") int offset);
}
